<%@page import="shmged.com.shlcm.workflow.TpSettings"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="shmged.com.shlcm.workflow.setparticipant.TpContainerHelper"%>
<%@page import="shmged.com.shlcm.workflow.WorkflowConfigBean"%>
<%@page import="shmged.com.shlcm.workflow.setparticipant.TpWorkflowHelper"%>
<%@ page import="java.util.List,java.util.ArrayList,java.util.Map,java.util.HashMap,java.util.Iterator,java.util.Locale,wt.util.WTContext,wt.util.WTMessage"%>
                 
<%@page import="wt.httpgw.GatewayServletHelper"%>
<%@page import="wt.httpgw.URLFactory"%>
<%@ page import="wt.org.WTUser"%>
<%@ page import="wt.org.WTGroup"%>
<%@ page import="wt.project.Role,wt.fc.PersistenceHelper,wt.fc.Persistable"%>
<%@ page import="wt.org.WTPrincipalReference"%>

<style type="text/css">
	.SpaceStyle{
		text-decoration:none;
	}
</style>

<%
String oid = request.getParameter("oid");
String isNeedSearchButton = request.getParameter("isNeedSearchButton");
boolean isNeedConfigPath = true;
WorkflowConfigBean wcb = null;
if(TpWorkflowHelper.isSetupParticipants(oid, "setupParticipants")){
	wcb = TpWorkflowHelper.getWorkflowConfigBean(oid,isNeedConfigPath);
}
if(wcb != null ){
	Locale locale = WTContext.getContext().getLocale();
	//String isNeedSearchButton = TpWorkflowHelper.isNeedSearchUserButton();
	
	String workflowResource = "shmged.com.shlcm.workflow.resources.WorkflowRB";
	String GET_CONFIGPATH_ERROR_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"GET_CONFIGPATH_ERROR",null,locale);
	String PRINCIPAL_TITLE = WTMessage.getLocalizedMessage(workflowResource,"PRINCIPAL_TITLE",null,locale);
	String ROLE_LIST = WTMessage.getLocalizedMessage(workflowResource,"ROLE_LIST",null,locale);
	String SELECT_USER = WTMessage.getLocalizedMessage(workflowResource,"SELECT_USER",null,locale);
	String ADD_BUTTON = WTMessage.getLocalizedMessage(workflowResource,"ADD",null,locale);
	String NOT_SUPORT_AJAX_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"NOT_SUPORT_AJAX",null,locale);
	String AJAX_ERROR_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"AJAX_ERROR",null,locale);
	String SELECT_USER_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"SELECT_USER",null,locale);
	String USER_EXIST_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"USER_EXIST",null,locale);
	String NEED_USER_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"NEED_USER",null,locale);
	String INPUT_USER_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"INPUT_USER",null,locale);
	String SEARCH_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"SEARCH",null,locale);
	String USER_NAME_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"USER_NAME",null,locale);
	
	String SINGATURE_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"SINGATURE",null,locale);
	String NEED_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"NEED",null,locale);
	String SELECTABLE_MESSAGE = WTMessage.getLocalizedMessage(workflowResource,"SELECTABLE",null,locale);
	String SETPARTICIPANT_WRONGROLEMESSAGE = WTMessage.getLocalizedMessage(workflowResource,"SETPARTICIPANT_WRONGROLEMESSAGE",null,locale);	
	String JUNDAIBIAO = WTMessage.getLocalizedMessage(workflowResource,"JUNDAIBIAO",null,locale);	
	
	String YONGHUFANGDAIBIAO = WTMessage.getLocalizedMessage(workflowResource,"YONGHUFANGDAIBIAO",null,locale);	
	String FZSYSZGLD = WTMessage.getLocalizedMessage(workflowResource,"FZSYSZGLD",null,locale);	
	
	String XITONGZHUGUANLINGDAO = WTMessage.getLocalizedMessage(workflowResource,"XITONGZHUGUANLINGDAO",null,locale);	
	String ZONGGONGCHENGSHI = WTMessage.getLocalizedMessage(workflowResource,"ZONGGONGCHENGSHI",null,locale);	
	
	
	String contextPath = request.getContextPath();
	Map containerRoleUser = TpContainerHelper.getRoleAndUserByContainerName(TpSettings.getSection("participantConfig").get("manageContainerName", "管理项目项目"),true);	
	List<String> roleList = new ArrayList<String>();
	
		
		roleList = wcb.getRoleList(oid);
		List<String> titleList = wcb.getTitleList();
		Map<String,Object> contentMap = wcb.getContentMap();
		if(roleList != null){
	Map workflowRoleUser = null;
	workflowRoleUser = TpWorkflowHelper.getUserByRole(oid,roleList);
%>
			
<script>
	var oid = '<%=oid%>';
	var locale = '<%=locale%>';
	var isNeedConfigPath = '<%=isNeedConfigPath%>';
	var containerRoleList = new Array();
	var addPrincipalMap = new Array();
	var buttonFlag = true;
	function allPrpos(obj) { 
var props = "";
for(var p in obj){ 
if(typeof(obj[p])=="function"){ 
obj[p]();
}else{ 
props+= p + "=" + obj[p] + "\t";
} 
} 
alert(props);
}

	function validata(){
		var flag = true;
					if(isNeedConfigPath == 'true'){
				for(var i = 0 ; i < addPrincipalMap.length ; i++){
					if (addPrincipalMap[i].mustAdd == "*" ){
						if(addPrincipalMap[i].userArray == 0){
							flag = false;
							alert(addPrincipalMap[i].roleDisName+" "+'<%=NEED_USER_MESSAGE%>');
							break;
						}
					}
				}
			}
		if(flag){
			//document.getElementsByName("completehidden")[0].onclick();
			//alert("validata ok ,could submit task!");
			//saveUser();
		}
		return flag;
	}
	
		function searchUser(){
			var userName = document.getElementById("userName").value;
			if(userName == ""){
				alert("<%=INPUT_USER_MESSAGE%>");
			}else{
				var selectElement = document.getElementById("containerRoleList");
				selectElement.selectedIndex = -1;
				userName=encodeURI(userName );
				
				var xmlHttpRequest;
				if (window.XMLHttpRequest) { // Mozilla, Safari,...
        	xmlHttpRequest = new XMLHttpRequest();
        	if (xmlHttpRequest.overrideMimeType) {
        		xmlHttpRequest.overrideMimeType('text/xml');
        	}
      	} else if (window.ActiveXObject) { // IE
      		try {
        		xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        	} catch (e) {
        		try {
          		xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
          	    } catch (e) {
          		alert('<%=NOT_SUPORT_AJAX_MESSAGE%>');
          	    }
       		}
      	}
       	
				xmlHttpRequest.onreadystatechange=function(){
			  	if(xmlHttpRequest.readyState==4){
				  	if (xmlHttpRequest.status == 200) {
				  		var userNameList = xmlHttpRequest.responseText;
				  		var userNameArray = userNameList.split("`");
							updateSearchUser(userNameArray);
				  	}else{
				  		alert("<%=AJAX_ERROR_MESSAGE%>");
				  	}
				  }
				}
				xmlHttpRequest.open("GET","<%=contextPath%>/shmged/jsp/com/shlcm/workflow/setparticipant/searchUser.jsp?userName="+userName+"&locale="+locale+"&oid="+oid,true);
				xmlHttpRequest.setRequestHeader("If-Modified-Since","0");
				xmlHttpRequest.send(null);
			}
		}
		
		function updateSearchUser(userArray){
			var userList = document.getElementById("userList");
			userList.length=0;
			for(var i = 1 ; i <userArray.length-1;i++){
				var str = userArray[i];
				userList.options.add(new Option(str,str));
			}
			userList.selectedIndex = 0;
		}
	
	function saveUser(){
		var value = "";
		for(var i = 0 ; i < addPrincipalMap.length; i ++){
			value = value+addPrincipalMap[i].roleName;
			for(var j = 0 ; j < addPrincipalMap[i].userArray.length; j ++){
				value = value + "`"+addPrincipalMap[i].userArray[j];
			}
			if( i < addPrincipalMap.length-1){
				value = value + ":";
			}
		}
	
		var xmlHttpRequest;
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
       		xmlHttpRequest = new XMLHttpRequest();
        	if (xmlHttpRequest.overrideMimeType) {
        		xmlHttpRequest.overrideMimeType('text/xml');
        	}
      	} else if (window.ActiveXObject) { // IE
      		try {
        		xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        	} catch (e) {
        		try {
          			xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
          		} catch (e) {
          			alert("<%=NOT_SUPORT_AJAX_MESSAGE%>");
          		}
       		}
      	}

		xmlHttpRequest.onreadystatechange=function(){
			if(xmlHttpRequest.readyState==4){
				if (xmlHttpRequest.status == 200) {
					//alert(message[2]);
					var completehiddenBtn = document.getElementsByName("completehidden")[0];
					if (completehiddenBtn) {          	
						completehiddenBtn.onclick();
					}
				}else{
					alert("<%=AJAX_ERROR_MESSAGE%>");
				}
			}
	 	}

		value=encodeURI(value);
		value=encodeURI(value);

		xmlHttpRequest.open("GET","<%=contextPath%>/shmged/jsp/com/shlcm/workflow/setparticipant/handle.jsp?value="+value+"&oid="+oid,true);
		xmlHttpRequest.setRequestHeader("If-Modified-Since","0");
		xmlHttpRequest.send(null);
	}
	
	function showUser(){
		var containerSelect = document.getElementById("containerRoleList");
		var userListSelect = document.getElementById("userList");
		userListSelect.length = 0 ;
		var tempUserList = containerRoleList[containerSelect.selectedIndex].userArray;
		for(var i = 0 ; i < tempUserList.length ; i++){
			userListSelect.options.add(new Option(tempUserList[i].userName,tempUserList[i].userName));
		}
	}
	
	function isExist(index,value){
		var flag = false;
		for(var i = 0 ; i < addPrincipalMap[index].userArray.length ; i++){
			if(addPrincipalMap[index].userArray[i] == value){
				flag = true;
				break;
			}
		}
		return flag;
	}
	
	function addUser(index){
		if(buttonFlag != true){
			return ;
		}
		buttonFlag = false;
		var userListSelect = document.getElementById("userList");
		if(userListSelect.selectedIndex == -1){
			alert("<%=SELECT_USER_MESSAGE%>");
			buttonFlag = true;
			return;
		}
		var containerRoleSelect=document.getElementById("containerRoleList");
		//alert(containerRoleSelect[containerRoleSelect.selectedIndex].text);
		//alert(userListSelect[userListSelect.selectedIndex].text);
		//alert(addPrincipalMap[index].needRole);
		if(!isExist(index,userListSelect[userListSelect.selectedIndex].text)){
			var needRoles = addPrincipalMap[index].needRole;
			if(needRoles!=null&&needRoles!=""&&!(needRoles.indexOf(containerRoleSelect[containerRoleSelect.selectedIndex].text)>=0)){
				//alert("<%=JUNDAIBIAO%>");
				var jundaibiao = "<%=JUNDAIBIAO%>";
				var yonghufangdaibiao = "<%=YONGHUFANGDAIBIAO%>";
				var fzsyszgld = "<%=FZSYSZGLD%>";

				var zonggongchengshi = "<%=ZONGGONGCHENGSHI%>";
				var xitongzhuguanlingdao = "<%=XITONGZHUGUANLINGDAO%>";

				if(needRoles.indexOf(jundaibiao) >= 0){
					if(containerRoleSelect[containerRoleSelect.selectedIndex].text.indexOf(jundaibiao) < 0){					
						alert("<%=SETPARTICIPANT_WRONGROLEMESSAGE%>"+yonghufangdaibiao);
						buttonFlag = true;
						return;
					}
				}else if(needRoles.indexOf(fzsyszgld) >= 0){
					if(containerRoleSelect[containerRoleSelect.selectedIndex].text.indexOf(zonggongchengshi) < 0){
							if(containerRoleSelect[containerRoleSelect.selectedIndex].text.indexOf(xitongzhuguanlingdao) < 0){					
							alert("<%=SETPARTICIPANT_WRONGROLEMESSAGE%>"+fzsyszgld);
							buttonFlag = true;
							return;
						}
					}
				}
				else{				
					alert("<%=SETPARTICIPANT_WRONGROLEMESSAGE%>"+needRoles);
					buttonFlag = true;
					return;
				}
			}			
			addPrincipalMap[index].userArray[addPrincipalMap[index].userArray.length] = userListSelect[userListSelect.selectedIndex].text;
			//saveUser();
			var dE = document.getElementById("role"+index);
			var tempCount = addPrincipalMap[index].userArray.length-1;
			var temp = index+","+tempCount;
			var tempID = "role"+index+"a"+tempCount;
			//alert(userListSelect[userListSelect.selectedIndex].text);
			//alert(tempID);
			dE.innerHTML=dE.innerHTML+"<a href='javascript:removeUser("+temp+")' id='"+tempID+"'>"+userListSelect[userListSelect.selectedIndex].text+";<span>&nbsp</span></a>";
		}else{
			alert("<%=USER_EXIST_MESSAGE%>");
			buttonFlag = true;
		}
		buttonFlag = true;
	}
	
	function removeUser(roleIndex,userIndex){
		if(buttonFlag != true){
			return ;
		}
		buttonFlag = false;
		addPrincipalMap[roleIndex].userArray.splice(userIndex,1);
		//saveUser();
		var divE = document.getElementById("role"+roleIndex);
		var aE = document.getElementById("role"+roleIndex+"a"+userIndex);
		divE.removeChild(aE);
		var childrens = divE.children;
		var count = userIndex;
		for(var i = userIndex ; i < childrens.length ; i++){
			if(childrens[i].href != undefined && childrens[i].href != ""){
				childrens[i].href = "javascript:removeUser("+roleIndex+","+count+")";
				childrens[i].id = "role"+roleIndex+"a"+count;
				count++;
			}
		}
		buttonFlag = true;
	}
	
	function changePath(){

		var path_check = document.getElementsByName("path_check");
		//var buttonRole = document.getElementById("buttonRole"+index);
		for(var i = 0 ; i < path_check.length ; i++){
			for(var j = 0 ; j < addPrincipalMap.length ; j++){
				if(addPrincipalMap[j].path == path_check[i].id){
					if(path_check[i].checked == true){
						addPrincipalMap[j].changePath = "1";
					}else{
						addPrincipalMap[j].changePath = "";
					}
				}
			}
		}
		
		//	buttonRole.disabled = false;
		//}else{
		//	buttonRole.disabled = true;
			//addPrincipalMap[index].userArray.length = 0;
		//	var divE = document.getElementById("role"+index);
			//dieE.innerHTML = "";
			//saveUser();
		//}
	}
	
	function initalPath(){
		var path_check = document.getElementsByName("path_check");
		for(var i = 0 ; i < path_check.length ; i++){
			changePath(i);
		}
		path_check[0].disabled = true;
		path_check[path_check.length-2].disabled = true;
		path_check[path_check.length-1].disabled = true;
	}
</script>
	<B><%=PRINCIPAL_TITLE%></B>
	<table>
		<tr>
			<td><B><%=SINGATURE_MESSAGE%>:</B></td>
		<%
			List<String> selectableList = new ArrayList<String>();
			for(int i = 1 ; i < titleList.size(); i++){
				String tempTitle = titleList.get(i);
				String tempContent = (String)contentMap.get(tempTitle);
				String returnStr=TpWorkflowHelper.definePageDisplyProcess(tempContent);
				if(returnStr != null&&!"".equals(returnStr)){
		%>
		<tr>
			<td></td>
			<td align="right">
				<B><%=tempTitle%>:</B>
			</td>
			<td><%=returnStr%></td>
	</tr>

		<%
			}
			}
		%>
		</tr>
		
	</table>
	
<table  width="100%" border="0">
	<tr>
		<td align="right">
		</td>
		<td>
		</td>
		<td></td>
	</tr>
<%if("true".equals(isNeedSearchButton)){%>
		<tr>
			<td  width="200"><B><%=USER_NAME_MESSAGE%>:</B><input type="text" name="userName" value="" size="10"></td>
			<td  width="200"><input type="button" value="<%=SEARCH_MESSAGE%>" onclick="searchUser()"></td>
		</tr>
<%}%>
	<!--<tr>
		<td width="15%"><B><label><%=ROLE_LIST%></label></B></td>
		<td width="15%"><B><label><%=SELECT_USER%></label></B></td>
		<td></td>
	</tr>-->

	<tr>
		<td valign="top" height="100%">
			<select name="containerRoleList" id="containerRoleList" multiple="multiple" style="{width:170;height:300}" onChange="showUser()" >
			<%
				Iterator it1 = containerRoleUser.keySet().iterator();
					while(it1.hasNext()){
						Role role = (Role)it1.next();
						String roleCHName = role.getDisplay();
						String roleENName = role.toString();
			%>
				<option value="<%=roleENName%>"><%=roleCHName%></option>
				<script type="text/javascript">
					var tempContainerObj = new Object();
					tempContainerObj.roleName='<%=roleENName%>';
					tempContainerObj.roleDisName='<%=roleCHName%>';
					var tempContainerObjArray = new Array();
				</script>
				<%
					List userList = (List)containerRoleUser.get(role);
							for(int i = 0 ; i < userList.size();i++){
								WTUser user = (WTUser)userList.get(i);
								String userName = user.getFullName()+"("+user.getName()+")";
				%>
				<script type="text/javascript">
					var obj = new Object();
					obj.userName = '<%=userName%>';
					obj.userRoleName='<%=roleCHName%>';
					tempContainerObjArray[tempContainerObjArray.length] = obj;
				</script>
				<%
					}
				%>
				<script type="text/javascript">
					tempContainerObj.userArray=tempContainerObjArray;
					containerRoleList[containerRoleList.length]=tempContainerObj;
				</script>
			<%
				}
			%>
			</select>
		</td>
		<td valign="top">
			<div id="userListDiv" height="100%">
				<select name="userList" id="userList" multiple="multiple" style="{width:160;height:300;}">
				</select>
			</div>
		</td>
		<td width="100%">
			<table border = "0">
			<%
				for(int i = 0 ;roleList != null && i < roleList.size(); i++){
						String tempTitle = titleList.get(i+1);
						Role tmpRole = Role.toRole(roleList.get(i));
						String tmpDis = tmpRole.getLocalizedMessage(locale);
						String tempContent = (String)contentMap.get(tempTitle);
						
						tempContent = tempContent.replaceAll("[ \n\r]", "");
						
						String roleName = (String)roleList.get(i);
						String returnValue = TpWorkflowHelper.defineWhetherMust(tempContent);
						String needRole = TpWorkflowHelper.specRoleStr(tempContent);
			%>
						<!--<option value='<%=tmpRole.toString()%>'><%=tmpDis%></option>-->
				<script>
					var tempRoleAndUserObj = new Object();
					tempRoleAndUserObj.roleName='<%=roleName%>';
					tempRoleAndUserObj.roleDisName='<%=tmpDis%>';
					tempRoleAndUserObj.path = '<%=tempTitle%>';
					tempRoleAndUserObj.value = '<%=tempContent%>';
					tempRoleAndUserObj.changePath = '';
					tempRoleAndUserObj.needRole='<%=needRole%>';
					tempRoleAndUserObj.mustAdd='<%=returnValue%>';
					var tempRoleAndUserObjArray = new Array();
				</script>
				<tr>
					<td nowrap="nowrap">
						<div id="<%=tmpDis%>" >
							<input type="button" id="buttonRole<%=i%>" value="<%=ADD_BUTTON%>" onclick="addUser('<%=i%>')">
								&nbsp;<%=returnValue%><%=tmpDis%>:
						</div>
					</td>
					<td >
						<div width="100%" id="role<%=i%>">
						<%
							List roleUserList = (List)workflowRoleUser.get(tmpRole);
							for(int j = 0 ; roleUserList != null && j < roleUserList.size(); j++){
								WTUser user = (WTUser)roleUserList.get(j);
								String userName = user.getFullName()+"("+user.getName()+")";
						%>
							<a href="javascript:removeUser(<%=i%>,<%=j%>);" id="role<%=i%>a<%=j%>" name="<%=userName%>"><%=userName%>;</a>
							<script>
								tempRoleAndUserObjArray[tempRoleAndUserObjArray.length] = '<%=userName%>';
							</script>
						<%		
							}
						%>
						</div>
					</td>
				</tr>
				<script>
					tempRoleAndUserObj.userArray = tempRoleAndUserObjArray;
					addPrincipalMap[addPrincipalMap.length]=tempRoleAndUserObj;
				</script>
			<%		
				}
			%>
			</table>
		</td>
	</tr>
</table>
<script>
//initalPath();
</script>
<%
}
	}
%>